package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import com.itheima.utils.MyThreadLocalUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 部门管理Controller
 */
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result selectAll(){
        Map<String, Object> value = MyThreadLocalUtils.getValue();
        System.out.println("我们获取的密钥是=========================================" +
                "========================"+value);
        return Result.success(deptService.selectAll());
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        int i = deptService.deleteById(id);
        return i>0?Result.success():Result.error("删除失败");
    }

    @PostMapping
    public Result insertDept(@RequestBody Dept dept){
        int i = deptService.insertDept(dept);
        return i>0?Result.success():Result.error("id为:"+dept.getId()+"的部门添加失败");
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Dept dept = deptService.selectById(id);
        return dept!=null?Result.success(dept):Result.error("id为:"+id+"的部门不存在");
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        int i = deptService.updateDept(dept);
        return i>0?Result.success(dept):Result.error("id为:"+dept.getId()+"的部门修改失败");
    }
}
